package com.android.support.lib.universal.sdk.weixin.base;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

import com.android.support.lib.universal.sdk.weixin.wxapi.Constants;
import com.android.support.lib.universal.sdk.weixin.wxapi.ParamUtil;
import com.android.support.lib.universal.sdk.weixin.wxapi.Util;
import com.android.support.lib.universal.sdk.weixin.wxapi.WeixinManager;
import com.android.support.lib.universal.util.Utilapk;
import com.android.support.lib.universal.util.Utilview;
import com.android.support.lib.universal.view.dialog.CustomProgressDialog;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 微信支付（新接口）; 要求：注册包名.wxapi.WXPayEntryActivity(调起支付对包名和类名没有要求，但是支付回调对包名和类名有要求)
 * 使用说明：注册包名.wxapi.WXPayEntryActivity继承该Activity即可。
 *
 * @author 彭治铭
 */
public class NewsWXPayEntryActivity extends FragmentActivity implements
        IWXAPIEventHandler {
    private IWXAPI api;
    ParamUtil paramUtil = new ParamUtil();
    private static final int WIN_XIN_PAY_FOR_SUCCESS = 0;
    private CustomProgressDialog dialog;// 进度条
    Handler handler = new Handler() {// 微信结果返回后，销毁当前Activity
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case WIN_XIN_PAY_FOR_SUCCESS:
                    if (((String) msg.obj).equals("0")) {
                        WeixinManager.getInstance().onPaySuccess();
                        NewsWXPayEntryActivity.this.onBackPressed();
                        finish();
                    } else {
                        WeixinManager.getInstance().onPayFailed();
                        NewsWXPayEntryActivity.this.onBackPressed();
                        finish();
                    }
                    break;
            }
        }

        ;
    };

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();// 关闭进度条
            }
        }
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        dialog = CustomProgressDialog.createDialog(NewsWXPayEntryActivity.this);
        dialog.show();

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        Utilapk utilapk = new Utilapk();
        if (utilapk.isWXAppInstalledAndSupported(api)) {
            api.handleIntent(getIntent(), this);
            GetPrepayIdTask getPrepayId = new GetPrepayIdTask();
            getPrepayId.execute();
        } else {
            this.finish();
        }
        utilapk = null;
    }

    private static enum LocalRetCode {
        ERR_OK, ERR_HTTP, ERR_JSON, ERR_OTHER
    }

    // 1.统一下单，获取预支付订单
    private class GetPrepayIdTask extends
            AsyncTask<Void, Void, GetPrepayIdResult> {

        @Override
        protected void onPostExecute(GetPrepayIdResult result) {
            if (result.localRetCode == LocalRetCode.ERR_OK) {
                sendPayReq(result);// 下单成功，正式调用微信支付接口
            } else {
                Toast.makeText(NewsWXPayEntryActivity.this,
                        "统一下单失败" + result.localRetCode.name(),
                        Toast.LENGTH_LONG).show();
                NewsWXPayEntryActivity.this.finish();
            }
        }

        @Override
        protected GetPrepayIdResult doInBackground(Void... params) {
            String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";// 统一下单，预支付订单
            String entity = paramUtil.genProductArgs_Unifiedorder(NewsWXPayEntryActivity.this, "APP");
            try {
                entity = new String(entity.getBytes(), "ISO8859-1");// 微信服务器是ISO8859-1的格式。
            } catch (UnsupportedEncodingException e) {
            }
            GetPrepayIdResult result = new GetPrepayIdResult();
            String content = Util.httpPost(url, entity);
//			 Log.e("test", "预支付订单信息" + content);
            if (content == null || content.length() == 0) {
                result.localRetCode = LocalRetCode.ERR_HTTP;
                return result;
            }
            result.parseFrom(content);// 对预下单数据进行解析
            return result;
        }
    }

    // 2.解析预下单数据
    private class GetPrepayIdResult {

        public LocalRetCode localRetCode = LocalRetCode.ERR_OTHER;
        public String prepayId;

        public void parseFrom(String content) {
            if (content == null || content.length() <= 0) {
                localRetCode = LocalRetCode.ERR_JSON;
                return;
            }
            try {
                List<String> list = new ArrayList<String>();
                list.add("prepay_id");
                Map<String, String> map = paramUtil.xmlPull(content, list,
                        "UTF-8");
                if (map != null && map.containsKey("prepay_id")) {
                    prepayId = map.get("prepay_id");
                    localRetCode = LocalRetCode.ERR_OK;
                } else {
                    localRetCode = LocalRetCode.ERR_JSON;
                }

            } catch (Exception e) {
                localRetCode = LocalRetCode.ERR_JSON;
            }
        }
    }

    // 3.正式调用微信支付接口
    private void sendPayReq(GetPrepayIdResult result) {

        PayReq req = new PayReq();
        req.appId = Constants.APP_ID;
        req.partnerId = Constants.PARTNER_ID;
        req.prepayId = result.prepayId;
        req.nonceStr = paramUtil.genNonceStr();// // 随机字符串，不长于32位，必须
        req.timeStamp = String.valueOf(System.currentTimeMillis() / 1000);// 时间戳,必不可少
        req.packageValue = "Sign=WXPay";// "Sign=" +
        // packageValue;暂填写固定值Sign=WXPay

        List<NameValuePair> signParams = new LinkedList<NameValuePair>();
        signParams.add(new BasicNameValuePair("appid", req.appId));
        signParams.add(new BasicNameValuePair("noncestr", req.nonceStr));
        signParams.add(new BasicNameValuePair("package", req.packageValue));
        signParams.add(new BasicNameValuePair("partnerid", req.partnerId));
        signParams.add(new BasicNameValuePair("prepayid", req.prepayId));
        signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));
        req.sign = paramUtil.createSign(signParams);
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.sendReq(req);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq arg0) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            handler.sendMessage(handler.obtainMessage(WIN_XIN_PAY_FOR_SUCCESS,
                    resp.errCode + ""));
        }
    }

}
